package com.itheima.health.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.utils.redis.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javafx.util.Duration;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("checkitem")
@Api(tags = "传智健康预约模块之检查项管理")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;





    // 修改 和 添加   判断   checkItem  有无和数据库记录相同 id
    @PostMapping("saveOrUpdate")
    public Result   saveOrUpdate(@RequestBody CheckItem checkItem,HttpServletRequest request){
//        Integer id = checkItem.getId();
//        if(checkItem.getId()!=null){
//            RedisTemplate<Object, Object> redisTemplate = RedisUtil.getInstance();
//            Boolean ifAbsent = redisTemplate.opsForValue().setIfAbsent(id+"", user);
//            if(ifAbsent){
//                //  第一次修改的人，完成修改操作，  设置过期时间
//                redisTemplate.expire(id+"", 15,TimeUnit.SECONDS);
//                return  new Result(checkItemService.saveOrUpdate(checkItem));
//            }else{
//                String o = (String)redisTemplate.opsForValue().get(id + "");
//                if(!user.equals(o)){
//                    return  new Result(false,o+"正在修改。。请稍后再试!");
//                }else{
//                    return  new Result(checkItemService.saveOrUpdate(checkItem));
//                }
//            }
//        }
        return  new Result(checkItemService.saveOrUpdate(checkItem));
    }


//    @GetMapping("isHot/{id}")
//    public Result   isHot(@PathVariable("id") int id){
//
//        RedisTemplate<Object, Object> redisTemplate = RedisUtil.getInstance();
//            Boolean ifAbsent = redisTemplate.opsForValue().setIfAbsent(id+"", user);
//            //  ifAbsent   true  表示 redis上个不存在 该值
//            if(ifAbsent){
//                //  第一次修改的人，完成修改操作，  设置过期时间
//                redisTemplate.expire(id+"", 15,TimeUnit.SECONDS);
//                return  new Result(true);
//            }else{
//                String o = (String)redisTemplate.opsForValue().get(id + "");
//                if(!user.equals(o)){
//                    return  new Result(false,o+"正在修改。。请稍后再试!");
//                }else{
//                    return  new Result(true);
//                }
//            }
//
//    }

    @DeleteMapping("deleteById/{id}")
    public Result   deleteById(@PathVariable("id") int id){
        return  new Result(checkItemService.updateCheckItemById(id));
    }

    @GetMapping("findAll")
    @ApiOperation(value = "查询功能",notes = "检查项查询列表信息")
    @PreAuthorize("hasAuthority('find')")
    public Result   findAll(){
        return  new Result(checkItemService.list());
    }

    @PostMapping("findPage")
    @ApiOperation(value = "查询功能",notes = "检查项查询列表信息")
    @PreAuthorize("hasAuthority('find')")
    public Result   findPage(@RequestBody QueryPageBean queryPageBean){
        return  new Result(checkItemService.findPage(queryPageBean));
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation(value = "删除功能",notes = "根据id 删除指定检查项")
    public Result   delete(@PathVariable("id") int id){
        return  new Result(checkItemService.removeById(id));
    }

}
